﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static LiWi.Shell;
namespace LiWi
{
    public partial class 连续插补 : Form
    {
        public WindowsAdapt adapt = new WindowsAdapt();
        IntPtr _handle { get { return Data.handle; } }
        bool isConnect { get { return Data.handle == (IntPtr)0 ? false : true; } }
        int[] axisList = { 0, 1, 2, 3 };

        float nowSpeed;
        int rebuf;                  //剩余缓冲
        int mark;                   //mark号




        public struct RunData
        {
            public float xPos;
            public float yPos;
            public float zPos;
            public float rPos;
            public float spd;

        }
        public 连续插补()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            string[] datas = new string[6];
            //Random ran = new Random();

            for (int i = 0; i < 20; i++)
            {
                datas[0] = Convert.ToString(i + 1);
                datas[1] = Convert.ToString((i + 1) * 100);
                datas[2] = Convert.ToString((i + 1) * 100);
                datas[3] = Convert.ToString((i + 1) * 100);
                datas[4] = Convert.ToString((i + 1) * 100);
                //datas[5] = Convert.ToString(ran.Next(100, 1000));   //生成随机速度10-500
                datas[5] = Convert.ToString(100);
                dataGridView_point.Rows.Add(datas);
            }
            numericUpDown2.Maximum = dataGridView_point.Rows.Count;
            adapt.SetForm(this);
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isConnect)
            {
                if (!GetIdle())
                {
                    enableContons(false);
                }
                else
                {
                    enableContons(true);
                }
                if (Data._pause) button_start.Enabled = false;
                

                PMC_GetVpSpeed(_handle, 0, ref nowSpeed);
                label_vspeed.Text = $"当前速度:{nowSpeed}";

                PMC_GetRemain_LineBuffer(_handle, 0, ref rebuf);
                label_buff.Text = $"剩余缓冲:{rebuf}";
                PMC_GetMoveCurmark(_handle, 0, ref mark);
                label_mark.Text = $"当前MARK:{mark}";
            }
            else
            {
                enableContons(true);
            }
        }

        public void enableContons(bool enable)
        {
            button_start.Enabled = enable;
            checkBox_changeSpd.Enabled = enable;
            checkBox_changeMerge.Enabled = enable;
            radrunMove1.Enabled = enable;
            radrunMove2.Enabled = enable;
            checkBox_corner.Enabled = enable;
            checkBox_corner2.Enabled = enable;
            checkBox_corner3.Enabled = enable;

            if (enable)
            {
                label_state.Text = "运动状态:停止";
                button_start.Text = "启 动";
            }
            else
            {
                label_state.Text = "运动状态:运动中";
                button_start.Text = "启 动";
            }
        }

        private void button_start_Click(object sender, EventArgs e)
        {
            if (isConnect)
            {
                PMC_Rapidstop(_handle, 3);
                for (int i = 0; i < 4; ++i)
                {
                    int enable = 0;
                    PMC_GetAxisEnable(_handle, i, ref enable);
                    if (enable == 0)
                    {
                        MessageBox.Show("请回连接处将需要运动的轴使能！！！");
                        return;
                    }
                }

                enableContons(false);
                PMC_Rapidstop(_handle, 3);
                for (int i = 0; i < axisList.Length; i++)
                {
                    PMC_SetAtype(_handle, i, 1);
                    PMC_SetSpeed(_handle, i, (float)numericUpDown_spd.Value);
                    PMC_SetAccel(_handle, i, (float)numericUpDown_accel.Value);
                    PMC_SetDecel(_handle, i, (float)numericUpDown_decel.Value);
                    PMC_SetUnits(_handle, i, (float)num_uint.Value);
                    PMC_SetMerge(_handle, i, Convert.ToInt16(checkBox_changeMerge.Checked));
                    PMC_SetSpeedDyna(_handle, i, Convert.ToInt16(checkBox_changeSpd.Checked));
                }
                int cmode = 0;
                if (checkBox_corner.Checked)
                {
                    cmode += 2;
                    PMC_SetDecelAngle(_handle, 0, (float)((float)numericUpDownstartAngle.Value * Math.PI / 180.0));
                    PMC_SetStopAngle(_handle, 0, (float)((float)numericUpDownStopAngle.Value * Math.PI / 180.0));
                }
                if (checkBox_corner2.Checked)
                {
                    cmode += 8;

                    PMC_SetFullSpRadius(_handle, 0, (float)numericUpDown_circR.Value);
                }
                if (checkBox_corner3.Checked)
                {
                    cmode += 32;
                    PMC_SetFullSpRadius(_handle, 0, (float)numericUpDown_ChamfR.Value);
                }
                PMC_SetCornerMode(_handle, 0, cmode);
                PMC_SetForceSpeed(_handle, 0, (float)numericUpDownLsp.Value);
                //运动点
                Data._pause = false;
                RunData runData = new RunData();
                PMC_Trigger(_handle);
                if (numericUpDown2.Value >= numericUpDown1.Value)
                {
                    Task.Factory.StartNew(async () =>
                    {
                        for (int i = (int)numericUpDown1.Value - 1; i < numericUpDown2.Value; i++)
                        {
                            runData.xPos = Convert.ToSingle(dataGridView_point.Rows[i].Cells[1].Value.ToString());
                            runData.yPos = Convert.ToSingle(dataGridView_point.Rows[i].Cells[2].Value.ToString());
                            runData.zPos = Convert.ToSingle(dataGridView_point.Rows[i].Cells[3].Value.ToString());
                            runData.rPos = Convert.ToSingle(dataGridView_point.Rows[i].Cells[4].Value.ToString());
                            runData.spd = Convert.ToSingle(dataGridView_point.Rows[i].Cells[5].Value.ToString());

                            //判断缓冲剩余量:如果缓冲小于20等待一下               
                            while (Data.ReMainBuffer(axisList) <= 20)
                            {
                                Thread.Sleep(2000);
                            }
                            move(runData);
                        }
                    });
                }
                else
                {
                    MessageBox.Show("结束点应大于起始点");
                    return;
                }
            }
        }

        public void move(RunData runData)
        {
            var ret = 0;
            if (isConnect)
            {
                float[] destlist = new float[4];
                destlist[0] = runData.xPos;
                destlist[1] = runData.yPos;
                destlist[2] = runData.zPos;
                destlist[3] = runData.rPos;
                int remainBuffer = 0;

                while (remainBuffer > 0 && ret == 0)
                {
                    ret = PMC_GetRemain_LineBuffer(_handle, 0, ref remainBuffer);
                }
                // if (checkBox_changeSpd.Checked)
                ret = PMC_MovePara(_handle, 0, "SPEED", 0, runData.spd);
                if (radrunMove1.Checked)           //相对
                {

                    ret = PMC_Move(_handle, 4, axisList, destlist);
                    if (ret != 0)
                    {
                        return;
                    }
                }
                else if (radrunMove2.Checked)      //绝对
                {
                    ret = PMC_MoveAbs(_handle, 4, axisList, destlist);
                    if (ret != 0)
                    {
                        return;
                    }
                }
            }
        }


        private void button_zero_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 4; i++)
                PMC_SetDpos(_handle, i, 0);
        }


        private bool GetIdle()
        {
            if (isConnect)
            {
                int isIdle = -1;
                for (int i = 0; i < 4; i++)
                {
                    PMC_GetIfIdle(_handle, i, ref isIdle);
                    if (isIdle == 0)
                    {
                        break;
                    }
                }
                if (isIdle == 0)
                    return false;
                else
                    return true;
            }
            return true;
        }

        private void button_stop_Click(object sender, EventArgs e)
        {
            if (isConnect)
            {
                PMC_Rapidstop(_handle, 3);
                Data._pause = false;
            }
        }

        private void btn_pause_Click(object sender, EventArgs e)
        {
            if (!Data._pause)
            {
                PMC_MovePause(Data.handle, 0, 0);
                btn_pause.Text = "恢 复";
                Data._pause = true;
            }
            else
            {
                PMC_MoveResume(Data.handle, 0);
                btn_pause.Text = "暂 停";
                Data._pause = false;
            }

        }


    }
}
